﻿using Business;
using Model;
using Public;
using Public.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using wochu.DPS.API.Infrastructure;

namespace wochu.DPS.API.Controllers
{
    public class AccountController : ApiController
    {
        string SecondFloorUserCodes = ConfigHelper.GetSetting("SecondFloorUserCodes");
        private static BLogin _NewInstance = new BLogin();

        public static BLogin NewInstance
        {
            get
            {
                if (_NewInstance == null)
                {
                    _NewInstance = new BLogin();
                }

                return _NewInstance;
            }
        }

        [HttpGet]
        public HttpResponseMessage LogOn(string userName, string password)
        {
            if (string.IsNullOrWhiteSpace(userName))
                return ResultsExtensions.GetFailJson(Setting.Instance.getMessageByKey("U0001"));
            if (string.IsNullOrWhiteSpace(password))
                return ResultsExtensions.GetFailJson(Setting.Instance.getMessageByKey("U0002"));
            UserInfo userInfo = NewInstance.LogonUser(userName, password);
            if (userInfo != null)
            {
                string psd = SecureHelper.EnCrypt(password);
                if (psd.Equals(userInfo.PASSWORD))
                {
                    userInfo.PASSWORD = password;
                    if (userName == "sa" || userName == "system")
                    { userInfo.FLOOR = -1; }
                    else if (SecondFloorUserCodes.IndexOf(userName.Trim()) >= 0)
                    { userInfo.FLOOR = 2; }
                    else
                    { userInfo.FLOOR = 1; }
                    return ResultsExtensions.GetSuccessJson(userInfo);
                }
                else
                {
                    return ResultsExtensions.GetFailJson(Setting.Instance.getMessageByKey("U0003"));
                }
            }
            else
            {
                return ResultsExtensions.GetFailJson(Setting.Instance.getMessageByKey("U0004"));
            }
        }
    }
}
